package no.doomsday.scripting.simplescriptengine.scriptevents 
{
	import flash.utils.Dictionary;
	import no.doomsday.scripting.simplescriptengine.interfaces.IDisposable;
	import no.doomsday.scripting.simplescriptengine.ScriptCore;
	/**
	 * ...
	 * @author Andreas Rønning (andreas@doomsday.no)
	 */
	public class ScriptEvent implements IScriptExecutable,IDisposable
	{
		private var _arguments:Array;
		private var _invoker:String;
		public function ScriptEvent(command:String,core:ScriptCore) 
		{
			//TODO: Better validation
			_invoker = command.split("(")[0];
			if (!core.hasCommand(_invoker)) {
				//TODO: If the command doesn't exist, throw and error and respond accordingly
				core.print("\t!Warning!, no command mapped to invoker '" + _invoker + "'");
				//core.print("\tWarning, no command mapped to invoker '" + _invoker + "'");
			}
			if (command.indexOf("()") > -1) {
				_arguments = [];
			}else {
				var argsString:String = command.split("(")[1].split(")");
				_arguments = argsString.split(",");
				for (var i:int = 0; i < _arguments.length; i++) 
				{
					if (_arguments[i].length == 0) {
						_arguments.splice(i, 1);
					}
				}
			}
			
		}
		
		/* INTERFACE no.doomsday.scripting.simplescriptengine.interfaces.IDisposable */
		
		public function dispose():void
		{
			_arguments = null;
		}
		
		/* INTERFACE no.doomsday.scripting.simplescriptengine.scriptevents.IScriptExecutable */
		
		public function execute(scriptCore:ScriptCore):void
		{
			scriptCore.tryCommand(_invoker, _arguments);
		}
		
	}

}